package com.enhinck.dbutilweb.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据源表
 *
 * @author enhinck Generate auto created
 * @date 2021-03-03 10:52:04
 */
@Slf4j
@Api(description = "数据源表管理", tags = {"DataController"})
//@RestController
public class DataController {

    private static List<String> courseId = new ArrayList<>();
    private static List<String> courseIdList = new ArrayList<>();
    private static List<String> lessonId = new ArrayList<>();
    private static List<String> lessonIdList = new ArrayList<>();
    private static List<String> lessonIdOnCourse = new ArrayList<>();
    private static List<String> lessonIdPoint = new ArrayList<>();
    private static List<String> lessonIdsListPaper = new ArrayList<>();
    private static List<String> liveRoomIds = new ArrayList<>();
    private static List<String> teacherIds = new ArrayList<>();
    private static List<String> videoCodesList = new ArrayList<>();



    static {
        courseId = getFile("courseId");
        courseIdList = getFile("courseIdList");
        lessonId = getFile("lessonId");
        lessonIdList = getFile("lessonIdList");
        lessonIdOnCourse = getFile("lessonIdOnCourse");
        lessonIdPoint = getFile("lessonIdPoint");
        lessonIdsListPaper = getFile("lessonIdsListPaper");
        liveRoomIds = getFile("liveRoomIds");
        teacherIds = getFile("teacherIds");
        videoCodesList = getFile("videoCodesList");
    }

    /**
     * 删除
     *
     * @return
     */
    @ApiOperation("测试连接")
    @GetMapping("/getRandom/{type}")
    public String test(@PathVariable("type") String type) {
        switch (type) {
            case "courseId":
                return getRandom(courseId);
            case "courseIdList":
                return getRandom(courseIdList);
            case "lessonId":
                return getRandom(lessonId);
            case "lessonIdList":
                return getRandom(lessonIdList);
            case "lessonIdOnCourse":
                return getRandom(lessonIdOnCourse);
            case "lessonIdPoint":
                return getRandom(lessonIdPoint);
            case "lessonIdsListPaper":
                return getRandom(lessonIdsListPaper);
            case "liveRoomIds":
                return getRandom(liveRoomIds);
            case "teacherIds":
                return getRandom(teacherIds);
            case "videoCodesList":
                return getRandom(videoCodesList);
            default:
                return "";
        }
    }

    public static String getRandom(List<String> list) {
        int index = (int) (Math.random() * list.size());
        return list.get(index);
    }

    public static List<String> getFile(String name) {
        InputStream is = DataController.class.getClassLoader().getResourceAsStream(name+".txt");
        List<String> list = null;
        try {
            list = IOUtils.readLines(is,"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}